//
//  WATFHomeViewController.swift
//  WiFiFaster
//
//  Created by one on 2023/12/28.
//

import UIKit

class WATFHomeViewController: WATFBaseScrollViewController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        WATFManager.watf_manager.watf_ftvChangeBlock = {[weak self] watf_state in
            print(watf_state.watf_title)
            if watf_state == .watf_connected {
                self?.watf_ftvStateConnect = .watf_connected
                
            }
            if watf_state == .watf_connecting {
                self?.watf_speedView.watf_ftvStateView()
                self?.watf_bottomView.watf_ftvStateView()
            }
            if watf_state == .watf_disconnecting {
                self?.watf_speedView.watf_ftvStateView()
                self?.watf_bottomView.watf_ftvStateView()
            }
            if watf_state == .watf_disconnected {
                self?.watf_ftvStateConnect = .watf_disconnected

            }
            if watf_state == .watf_invalid {
                self?.watf_speedView.watf_ftvStateView()
                self?.watf_bottomView.watf_ftvStateView()
            }
        }
        WATFManager.watf_manager.watf_ftvAuthAction {[weak self] _, _ in
            self?.watf_ftvStateConnect = WATFManager.watf_manager.watf_ftvCurStateType
            self?.watf_speedView.watf_ftvStateView()
            self?.watf_bottomView.watf_ftvStateView()
        }
        
        NotificationCenter.default.addObserver(self, selector: #selector(watf_ftvListConnect), name: watf_ftvConnect_noti, object: nil)
    }
    // MARK: - watf action
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        watf_speedView.watf_updateStateView()
        watf_reloadFtvStateView()
    }
    override func watf_scrollViews() {
        super.watf_scrollViews()
        view.addSubview(watf_navView)
        watf_contentView.addSubview(watf_speedView)
        watf_contentView.addSubview(watf_bottomView)
        watf_contentView.snp.remakeConstraints { make in
            make.top.left.width.equalToSuperview()
            make.height.equalTo(watf_setScrollRect().height)
        }
        watf_speedView.snp.makeConstraints { make in
            make.size.equalTo(CGSize(width: watf_SCREEN_WIDTH-32, height: 354))
            make.centerX.equalToSuperview()
            make.top.equalToSuperview().offset(40)
        }
        watf_bottomView.snp.makeConstraints { make in
            make.width.equalTo(watf_SCREEN_WIDTH)
            make.centerX.equalToSuperview()
            make.top.equalTo(watf_speedView.snp.bottom).offset(-70)
            make.bottom.equalToSuperview().offset(-10)
        }
    }
    // MARK: - watf lazy
    lazy var watf_navView: WATFNavHomeView = {
        let watf_navView = WATFNavHomeView()
        return watf_navView
    }()
    lazy var watf_speedView: WATFHomeSpeedView = {
        let watf_speedView = WATFHomeSpeedView()
        watf_speedView.watf_iconView.watf_addTap(target: self, action: #selector(watf_ftvClick))
        return watf_speedView
    }()
    lazy var watf_bottomView: WATFHomeBottomView = {
        let watf_bottomView = WATFHomeBottomView()
        watf_bottomView.watf_ftvButton.addTarget(self, action: #selector(watf_ftvClick), for: .touchUpInside)
        return watf_bottomView
    }()
    var watf_scanTimeName: String = "watf_scanTimeName"
    var watf_scanTimeSum:CGFloat = 0
    var watf_ftvStateConnect: WATFFTVStateType = .watf_none
    var watf_disConClick = false
    var watf_ftvListClick = false
    var watf_showAd = false
    var watf_adDone = false
}

extension WATFHomeViewController {
    func watf_reloadFtvStateView() {
       watf_speedView.watf_ftvStateView()
       watf_bottomView.watf_ftvStateView()
    }
    @objc func watf_ftvClick() {
        watf_bottomView.watf_tipView.isHidden = true
        if !WATFManager.watf_manager.watf_isCanNetWork() {
            WATFHudCom.watf_showMessage("The current network is unavailable, please check the network", isError: false)
            return
        }
        if WATFManager.watf_manager.watf_ftvCurStateType != .watf_connected {
            WATFManager.watf_manager.watf_testAdWork = true
            WATFManager.watf_manager.watf_ftvWork = true
            WATFManager.watf_manager.watf_alertShow = true
            let watf_ftvModel = WATFManager.watf_manager.watf_getSelectFTVModel()
            WATFManager.watf_manager.watf_startConnect(watf_ftvModel: watf_ftvModel) {[weak self] watf_state in
                if watf_state {
                    self?.watf_disConClick = false
                    WATFManager.watf_manager.watf_ftvStateClickType = .watf_connect
                    WATFManager.watf_manager.watf_testAdWork = true
                    WATFManager.watf_manager.watf_ftvWork = true
                    WATFManager.watf_manager.watf_alertShow = true
                    self?.watf_showAd = false
                    self?.watf_adDone = false
                    self?.watf_ftvListClick = false
                    self?.watf_ftvStateConnect = .watf_none
                    self?.watf_startScanTimer()
                }else {
                    WATFManager.watf_manager.watf_testAdWork = false
                    WATFManager.watf_manager.watf_ftvWork = false
                    WATFManager.watf_manager.watf_alertShow = false
                }
            }
            
        }else {
            WATFManager.watf_manager.watf_ftvStateClickType = .watf_disConnect
            WATFManager.watf_manager.watf_testAdWork = true
            WATFManager.watf_manager.watf_ftvWork = true
            WATFManager.watf_manager.watf_alertShow = true
            watf_showAd = false
            watf_adDone = false
            watf_ftvListClick = false
            watf_ftvStateConnect = .watf_none
            watf_disConClick = true
            watf_speedView.watf_ftvAnimateView.isHidden = false
            
            watf_speedView.watf_iconView.image = UIImage(named: "watf_ftv_normal")
            watf_speedView.watf_iconView.isUserInteractionEnabled = false
            watf_bottomView.watf_ftvButton.isUserInteractionEnabled = false
            
            watf_startScanTimer()
            WATFManager.watf_manager.watf_stopConnect()
        }
    }
    @objc func watf_ftvListConnect() {
        watf_bottomView.watf_tipView.isHidden = true
        if !WATFManager.watf_manager.watf_isCanNetWork() {
            WATFHudCom.watf_showMessage("No Network Connection", isError: false)
            return
        }
        if WATFManager.watf_manager.watf_testAdWork {
            return
        }
        if WATFManager.watf_manager.watf_ftvCurStateType == .watf_connected {
            WATFManager.watf_manager.watf_connectSelectFTVAddress()
            WATFManager.watf_manager.watf_testAdWork = true
            WATFManager.watf_manager.watf_ftvWork = true
            WATFManager.watf_manager.watf_alertShow = true
            watf_disConClick = false
            watf_ftvListClick = true
            watf_startScanTimer()
            watf_showAd = false
            watf_adDone = false
            return
        }
        WATFManager.watf_manager.watf_connectSelectFTVAddress {[weak self] watf_state in
            if watf_state {
                WATFManager.watf_manager.watf_testAdWork = true
                WATFManager.watf_manager.watf_ftvWork = true
                WATFManager.watf_manager.watf_alertShow = true
                self?.watf_disConClick = false
                self?.watf_ftvListClick = true
                self?.watf_startScanTimer()
                self?.watf_showAd = false
                self?.watf_adDone = false
            }else {
                WATFManager.watf_manager.watf_testAdWork = false
                WATFManager.watf_manager.watf_ftvWork = false
                WATFManager.watf_manager.watf_alertShow = false
            }
        }
        
        
    }
    
    @objc func watf_startScanTimer() {
        watf_ftvStateConnect = .watf_none
        watf_scanTimeName = WATFTimerCom.watf_timeTask(self, selector: #selector(watf_scanSum), start: 0, interval: 1, repeats: true, async: false, name: watf_scanTimeName)
    }
    
    fileprivate func watf_stopScanTimer() {
        WATFTimerCom.watf_cancelTask(watf_scanTimeName)
        watf_scanTimeName = "watf_scanTimeName"
        watf_scanTimeSum = 0
        // 1.0.1
        WATFManager.watf_manager.watf_testAdWork = false
        WATFManager.watf_manager.watf_ftvWork = false
        watf_speedView.watf_ftvStateView()
        watf_bottomView.watf_ftvStateView()
    }
    
    @objc fileprivate func watf_scanSum() {
        watf_scanTimeSum = watf_scanTimeSum + 1
        if watf_disConClick {
            
            if watf_scanTimeSum >= 3 && (watf_ftvStateConnect == .watf_connected || watf_ftvStateConnect == .watf_disconnected){
                // watf_getViewGetVController?.navigationController?.pushViewController(WATFFTVResultViewController(watf_connected: watf_ftvStateConnect == .watf_connected), animated: true)
                watf_stopScanTimer()
                return
            }
            if watf_scanTimeSum >= 18 {
               
                if (watf_ftvStateConnect == .watf_connected || watf_ftvStateConnect == .watf_disconnected){
                    //  watf_getViewGetVController?.navigationController?.pushViewController(WATFFTVResultViewController(watf_connected: watf_ftvStateConnect == .watf_connected), animated: true)
                    watf_stopScanTimer()
                    return
                }
                watf_stopScanTimer()
                WATFManager.watf_manager.watf_stopConnect { _ in
                    //  self.watf_getViewGetVController?.navigationController?.pushViewController(WATFFTVResultViewController(watf_connected: self.watf_ftvStateConnect == .watf_connected), animated: true)
                }
                return
            }
            return
        }
        
        if watf_scanTimeSum >= 3 && (watf_ftvStateConnect == .watf_connected || watf_ftvStateConnect == .watf_disconnected){
            //   watf_getViewGetVController?.navigationController?.pushViewController(WATFFTVResultViewController(watf_connected: watf_ftvStateConnect == .watf_connected), animated: true)
            watf_stopScanTimer()
            return
        }
        if watf_scanTimeSum >= 18 {
            
            if (watf_ftvStateConnect == .watf_connected || watf_ftvStateConnect == .watf_disconnected){
                //   watf_getViewGetVController?.navigationController?.pushViewController(WATFFTVResultViewController(watf_connected: watf_ftvStateConnect == .watf_connected), animated: true)
                watf_stopScanTimer()
                return
            }
            watf_stopScanTimer()
            WATFHudCom.watf_showMessage("Connection timed out, please try again", isError: false)
            WATFManager.watf_manager.watf_stopConnect()
            WATFManager.watf_manager.watf_ftvWork = false
            WATFManager.watf_manager.watf_testAdWork = false
            return
        }
        
    }
}
